using System;
using System.Web.UI;
namespace JQueryControlToolkit.Controls
{
	[ConstructorNeedsTag(true), ControlBuilder(typeof(JQueryScriptBuilder)), ParseChildren(false), PersistChildren(true)]
	public class JQueryScript : JQueryAction, JSObject
	{
		private string jqueryFunction;
		[JQueryElement("", SerializeAs = SerializeAs.Is)]
		public JSList Parameters
		{
			get
			{
				return base.GetPropertyValue<JSList>("Parameters", null);
			}
			set
			{
				base.SetPropertyValue<JSList>("Parameters", value);
			}
		}
		public string TagName
		{
			get;
			set;
		}
		protected override string jQueryFunction
		{
			get
			{
				return this.jqueryFunction;
			}
		}
		protected override void AddParsedSubObject(object obj)
		{
			if (obj is LiteralControl)
			{
				if (this.Parameters == null)
				{
					this.Parameters = new JSList();
				}
				string[] stringParameters = ((LiteralControl)obj).Text.Trim().Split(new char[]
				{
					','
				}, StringSplitOptions.RemoveEmptyEntries);
				string[] array = stringParameters;
				for (int i = 0; i < array.Length; i++)
				{
					string parameter = array[i];
					this.Parameters.Add(new JSString(parameter));
				}
			}
			else
			{
				if (obj is JQueryScript)
				{
					this.Parameters.Add((JQueryScript)obj);
				}
				base.AddParsedSubObject(obj);
			}
		}
		public JQueryScript(string tagName)
		{
			if (!tagName.EndsWith(typeof(JQueryScript).Name))
			{
				this.TagName = tagName;
			}
			this.jqueryFunction = JQuery.GetPlugInMethod("JQuery:" + tagName);
		}
		protected override JQuery EnsureFunctionCall()
		{
			JQuery result;
			if (this.jquery == null && this.Parent is JQueryScript)
			{
				if (this.jqueryFunction != null)
				{
					result = (this.jquery = this.RenderFunctionCall(((JQueryScript)this.Parent).jquery));
				}
				else
				{
					base.EnsureFunctionCall();
					result = this.jquery;
				}
			}
			else
			{
				result = base.EnsureFunctionCall();
			}
			return result;
		}
		public override JQuery RenderFunctionCall(JQuery jquery)
		{
			this.jquery = base.RenderFunctionCall(jquery);
			for (int i = 0; i < this.Parameters.Count; i++)
			{
				JSObject parameter = this.Parameters[i];
				if (parameter is JQueryScript)
				{
					JQueryScript script = (JQueryScript)parameter;
					if (script.jqueryFunction == null)
					{
						this.Parameters[i] = new JSFunction(new JSObject[]
						{
							script.EnsureFunctionCall()
						});
					}
					else
					{
						script.EnsureFunctionCall();
					}
				}
			}
			return this.jquery;
		}
	}
}
